import { DarkTheme, DefaultTheme, ThemeProvider } from '@react-navigation/native';
import { useFonts } from 'expo-font';
import { Stack } from 'expo-router';
import * as SplashScreen from 'expo-splash-screen';
import { useEffect,  } from 'react';
import store, {AppDispatch} from "@/store/index"
import 'react-native-reanimated';
import {Provider, useDispatch} from 'react-redux';
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { RootSiblingParent } from 'react-native-root-siblings';
import { ActionSheetProvider } from "@expo/react-native-action-sheet"
import {Platform, Text, TextInput, TouchableWithoutFeedback, Keyboard} from 'react-native'
import { LoadingOverlay, loadingService } from "@/utils/loadingServer";
import * as Updates from 'expo-updates';


import { useColorScheme } from '@/hooks/useColorScheme';
import {initSDK} from "@/utils/ImCommon";
import {getGrainInfoDetail, getUserInfoDetail} from "@/store/slices/loginSlice";

// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();

export default function RootLayout() {
  const colorScheme = useColorScheme();
  const [loaded] = useFonts({
    SpaceMono: require('@/assets/fonts/SpaceMono-Regular.ttf'),
  });

  // 设置 Text 组件的默认样式
  Text.defaultProps = {
    ...(Text.defaultProps || {}),
    style: { color: '#2A2B30', fontSize: 16, },  // 将 '#FF0000' 替换为你想要的默认颜色
  };
    TextInput.defaultProps = TextInput.defaultProps || {};


    Text.defaultProps.allowFontScaling = false;
    TextInput.defaultProps.allowFontScaling = false;

    console.log('Update Channel:', Updates.channel);
    console.log('Runtime Version:', Updates.runtimeVersion);
    console.log('Check for update...');


    useEffect(() => {
        if (__DEV__) return; // 避免在开发模式下运行
        async function checkForUpdates() {
            try {
                const update = await Updates.checkForUpdateAsync();
                if (update.isAvailable) {
                    await Updates.fetchUpdateAsync();
                    await Updates.reloadAsync(); // 重新加载应用
                }
            } catch (error) {
                console.error("Error fetching updates", error);
            }
        }

        checkForUpdates();
    }, []);


  useEffect(() => {
    if (loaded) {
      SplashScreen.hideAsync();
    }
  }, [loaded]);

    if (!loaded) {
    return null;
  }

    return (
        <Provider store={store}>
            <ActionSheetProvider>
                <SafeAreaProvider>
                    <RootSiblingParent>
                        <Stack>
                            <Stack.Screen
                                name="(main)/(tabs)"
                                options={{
                                    headerShown: false,
                                }}
                            >

                            </Stack.Screen>
                        </Stack>
                    </RootSiblingParent>
                </SafeAreaProvider>
            </ActionSheetProvider>
            <LoadingOverlay ref={loadingService.spinnerRef} />
        </Provider>
  );
}


export const unstable_settings = {
    // Ensure any route can link back to `/`
    initialRouteName: 'login',
};
